package com.rerun.controller;

import com.rerun.common.controller.SuperController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * The type Photo controller.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/11 15:19
 */
@Controller
@RequestMapping("/photo")
public class PhotoController extends SuperController {
    /**
     * Read image.
     *
     * @param response  the response
     * @param imageName the image name
     * @deprecated
     */
    @RequestMapping("/{imageName}")
    public void readImage(HttpServletRequest request, HttpServletResponse response, @PathVariable("imageName") String imageName) {

        String imagePath = request.getSession().getServletContext().getRealPath("/static/images/") + imageName + ".png";
        try {
            File file = new File(imagePath);
            if (file.exists()) {
                DataOutputStream dos = new DataOutputStream(response.getOutputStream());
                try (DataInputStream dis = new DataInputStream(new FileInputStream(imagePath))) {
                    byte[] b = new byte[2048];
                    while ((dis.read(b)) != -1) {
                        dos.write(b);
                        dos.flush();
                    }
                    dis.close();
                }
                dos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
